<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class HomeController extends Controller {

	/* 空操作，用于输出404页面 */
	public function _empty(){
		$this->redirect('Index/index');
	}
 
    protected function _initialize(){
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
        
        //获取网站设置信息
    	$_Websetting=M('Websetting')->select();
		if(count($_Websetting)>0)
		{
			$_TOKEN    = $_Websetting[0]['apptoken']; // 填写你设定的key,微信公众平台填写的key
			$_APPID    = $_Websetting[0]['appid'];
			$_APPSECRET= $_Websetting[0]['appsecret']; 
			$this->assign("_websetting",$_Websetting[0]);
		}
    }

	/* 用户登录检测 */
	protected function login(){
		/* 用户登录检测 */
		is_login() || $this->error('您还没有登录，请先登录！', U('User/login'));
	}

	public function getTurnPictures($type=null)
	{
		$map['type']=$type;
		$picInfo=M('PictureTurn')->where($map)->select();
		for ($i=0; $i<count($picInfo); $i++)
		{
			$picInfo[$i]['rownum']=$i;
		}
		return $picInfo;
	}
	/**
	 * 获取控制器菜单数组
	 * @author 张坤
	 */
	final public function getMenusAll($controller = CONTROLLER_NAME) {
		$menus = session ( 'HOME_MENU_LIST.' . $controller );
		if (empty ( $menus )) {
			// 获取主菜单
			$where ['pid'] = array('neq',0);
			$where ['level'] = array('eq',2);
			$menus ['main'] = M ( 'Channel' )->where ( $where )->order ( 'sort asc' )->field ( 'id,title,url' )->select ();
			$menus ['child'] = array (); // 设置子节点
			// 子菜单
			foreach ( $menus ['main'] as $key => $item ) {
				// 获取二级分类的合法url
				$map ['pid'] = $item ['id'];
				$menuList = M ( 'Channel' )->where ( $map )->field ( 'id,pid,title,url' )->order ( 'sort asc' )->select ();
				$menus ['child'] [$item ['id']] = $menuList;
			}
			session ( 'HOME_MENU_LIST.' . $controller, $menus );
		}
		return $menus;
	}
	/**
	 * 获取控制器菜单数组
	 * @author 张坤
	 */
	final public function getMenusTop($controller = CONTROLLER_NAME) {
		$menustop = session ( 'HOME_MENUTOP_LIST.' . $controller );
		if (empty ( $menustop )) {
			// 获取主菜单
			$where ['pid'] = array('eq',0);
			$menustop = M ( 'Channel' )->where ( $where )->order ( 'sort asc' )->field ( 'id,title,url' )->select ();
			session ( 'HOME_MENUTOP_LIST.' . $controller, $menustop );
		}
		return $menustop;
	}
	
}
